package com.danciyixue.boot.client.unitStudyStats.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.dcyx.model.form.UnitStudyStatsForm;
import com.danciyixue.boot.dcyx.model.query.UnitStudyStatsQuery;
import com.danciyixue.boot.dcyx.model.vo.UnitStudyStatsVO;
import com.danciyixue.boot.dcyx.service.UnitStudyStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端单元学习统计控制器
 */
@Tag(name = "12. 单元学习统计", description = "单元学习统计")
@RestController
@RequestMapping("/api/client/v1/unit-study-stats")
@RequiredArgsConstructor
public class DcyxUnitStudyStatsController {

    private final UnitStudyStatsService unitStudyStatsService;

    /**
     * 获取单元学习统计数据
     */
    @Operation(summary = "单元学习统计分页列表")
    @GetMapping("/page")
    public PageResult<UnitStudyStatsVO> page(UnitStudyStatsQuery queryParams) {
        IPage<UnitStudyStatsVO> page = unitStudyStatsService.getUnitStudyStatsPage(queryParams);
        return PageResult.success(page);
    }


    @Operation(summary = "新增单元学习统计")
    @PostMapping
    public Result<Void> saveUnitStudyStats(@RequestBody @Valid UnitStudyStatsForm formData ) {
        boolean result = unitStudyStatsService.saveUnitStudyStats(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取单元学习统计表单数据")
    @GetMapping("/{id}/form")
    public Result<UnitStudyStatsForm> getUnitStudyStatsForm(
            @Parameter(description = "单元学习统计ID") @PathVariable Long id
    ) {
        UnitStudyStatsForm formData = unitStudyStatsService.getUnitStudyStatsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改单元学习统计")
    @PutMapping(value = "/{id}")
    public Result<Void> updateUnitStudyStats(
            @Parameter(description = "单元学习统计ID") @PathVariable Long id,
            @RequestBody @Validated UnitStudyStatsForm formData
    ) {
        boolean result = unitStudyStatsService.updateUnitStudyStats(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除单元学习统计")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteUnitStudyStatss(
            @Parameter(description = "单元学习统计ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = unitStudyStatsService.deleteUnitStudyStatss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "新增OR更新单元学习统计")
    @PostMapping("/update")
    public Result<Void> saveOrUpdateUnitStudyStats(@RequestBody @Valid UnitStudyStatsForm formData) {
        boolean result = unitStudyStatsService.saveOrUpdateUnitStudyStats(formData);
        return Result.judge(result);
    }
}